#include <iostream>
#include "../../common/log.hpp"
#include <gflags/gflags.h>
#include "issearch.hpp"

DEFINE_bool(run_mode, false, "程序运行模式 false-调试 true-发布");
DEFINE_string(log_file, "log.txt", "发布模式下 输出文件的，文件名");
DEFINE_int32(log_level, 0 , "日志等级");


int main(int argc, char* argv[])
{
    google::ParseCommandLineFlags(&argc,&argv, true);
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    std::shared_ptr<elasticlient::Client> client(new elasticlient::Client({"http://127.0.0.1:9200/"}));
    
    // 创建索引
    EsIndex index(client, "usr", "doc");
    index.append("nickname");
    index.append("phone", "keyword", "standard", true);
    index.create();

    // 增加数据
    EsInsert tmp(client, "test_user", "_doc");
    tmp.append("nickname", "张三");
    tmp.append("phone", "17788889999");
    if(tmp.insert("00001") == 0)
    {
        ERROR_LOG("数据插入失败");
        return -1;
    }

    // 修改数据
    EsInsert tmp2(client, "test_user", "_doc");
    tmp2.append("nickname", "张三");
    tmp2.append("phone", "16655554444");
    if(tmp2.insert("00001") == 0)
    {
        ERROR_LOG("数据修改失败");
        return -1;
    }

    EsSearch tmp3(client, "test_user", "_doc");
    tmp3.append_should_terms("phone.keyword", "16655554444");
    // tmp3.append_must_not_terms("nickname.keyword", {"张三"});
    auto rsp = tmp3.search();
    if(rsp.empty() || rsp.isArray() == 0)
    {
        ERROR_LOG("检索失败");
        return -1;
    }
    int sz = rsp.size();
    for(int i = 0; i < sz; i++)
    {
        std::cout << rsp[i]["_source"]["nickname"].asString() << std::endl;
    }

        // 修改数据
    EsRemove tmp4(client, "test_user", "_doc");
    if(tmp4.remove("00001") == 0)
    {
        ERROR_LOG("数据删除失败");
        return -1;
    }

    return 0;
}